class: center, middle, inverse, title-slide # Computing in R ## AMC Graduate School ### Perry Moerland ### 2019-01-24 (updated: 2019-02-25) --- background-image: url(https://upload.wikimedia.org/wikipedia/commons/thumb/1/1b/R_logo.svg/500px-R_logo.svg.png) background-size: cover ??? Image credit: [Wikimedia Commons](https://commons.wikimedia.org/wiki/File:Sharingan_triple.svg) --- # Course setup - Course aim: become familiar with the basics of R - Two days, two sessions per day: 9:30-12:30 & 13:30-16:30 - Mix of interactive lectures and computer exercises -- - [Course website](http://wiki.bioinformaticslaboratory.nl/foswiki/bin/view/BioLab/ComputinginR) .footnote[ [1] http://wiki.bioinformaticslaboratory.nl/foswiki/bin/view/BioLab/ComputinginR ] -- - Comments and suggestions for improvement are most welcome --- # Stages in statistical analysis 1. <span style="color:red">Importing</span> data into statistical program 1. <span style="color:red">Inspecting</span> data - finding errors, cleaning - recoding and transforming - describing and summarizing data using spreadsheets, tables and graphics 1. <span style="color:red">Analyzing</span> data: estimation, uncertainty (confidence intervals, p-value), predictive value 1. <span style="color:red">Validating</span> model <br> Check the assumptions of the model 1. <span style="color:red">Reporting</span> of results <br> summary, tables, graphics <br> export --- # R as a pocket calculator - First of all, R can be used as a pocket calculator - Many mathematical operations are pre-defined in R <br> ```r > 2+7 ``` ``` # [1] 9 ``` -- ```r > sqrt(2) ``` ``` # [1] 1.414214 ``` ```r > cos(pi) ``` ``` # [1] -1 ``` ```r > log10(10^3) ``` ``` # [1] 3 ``` --- # Matrices .left-column[  ] .right-column[ ```r A <- matrix(data = 1:9, nrow = 3, ncol = 3) A[2,] ``` ``` # [1] 2 5 8 ``` ```r A[,1] ``` ``` # [1] 1 2 3 ``` ```r A[2,3] ``` ``` # [1] 8 ``` ] --- # Constructing a data frame ```r > pclass <- c("1st","2nd","1st") > survived <- c(1,1,0) > name <- c("Elisabeth Walton","Hudson Trevor","Helen Loraine") > age <- c(29.0,0.9167,2.0) > titanic <- data.frame(pclass,survived,name,age) > titanic ``` ``` # pclass survived name age # 1 1st 1 Elisabeth Walton 29.0000 # 2 2nd 1 Hudson Trevor 0.9167 # 3 1st 0 Helen Loraine 2.0000 ``` --- # R plots ```r > par(mar = c(4, 4, 1, .1)) > x <- (0:100)/10 > plot(x, x^3 - 13 * x^2 + 39 * x) ``` <!-- --> --- # Enhancing a plot .pull-left[ ```r x<-(0:100)/10 plot(x,x^3-13*x^2+39*x, type="l", xlab="time (hours)", ylab="temperature", cex.axis=1.5,cex.lab=1.5) ``` ] .pull-right[  ] --- # Enhancing a plot .pull-left[ ```r x<-(0:100)/10 plot(x,x^3-13*x^2+39*x, type="l", xlab="time (hours)", ylab="temperature", cex.axis=1.5,cex.lab=1.5) *points(2,34,col="red",pch=16, * cex=2) ``` ] .pull-right[  ] --- # Enhancing a plot .pull-left[ ```r x<-(0:100)/10 plot(x,x^3-13*x^2+39*x, type="l", xlab="time (hours)", ylab="temperature", cex.axis=1.5,cex.lab=1.5) points(2,34,col="red",pch=16, cex=2) *arrows(4,50,2.2,34.5) ``` ] .pull-right[  ] --- # Enhancing a plot .pull-left[ ```r x<-(0:100)/10 plot(x,x^3-13*x^2+39*x, type="l", xlab="time (hours)", ylab="temperature", cex.axis=1.5,cex.lab=1.5) points(2,34,col="red",pch=16, cex=2) arrows(4,50,2.2,34.5) *text(4.15,50,"local maximum", * adj=0,col="blue",cex=1.5) ``` ] .pull-right[  ] --- # Enhancing a plot .pull-left[ ```r x<-(0:100)/10 plot(x,x^3-13*x^2+39*x, type="l", xlab="time (hours)", ylab="temperature", cex.axis=1.5,cex.lab=1.5) points(2,34,col="red",pch=16, cex=2) arrows(4,50,2.2,34.5) text(4.15,50,"local maximum", adj=0,col="blue",cex=1.5) *lines(x,30-50*sin(x/2), * col="blue") ``` ] .pull-right[  ] --- # Enhancing a plot .pull-left[ ```r x<-(0:100)/10 plot(x,x^3-13*x^2+39*x, type="l", xlab="time (hours)", ylab="temperature", cex.axis=1.5,cex.lab=1.5) points(2,34,col="red",pch=16, cex=2) arrows(4,50,2.2,34.5) text(4.15,50,"local maximum", adj=0,col="blue",cex=1.5) lines(x,30-50*sin(x/2), col="blue") *legend(x=0,y=80, * legend=c("Sahara", * "Gobi"), * lty=1, * col=c("black","blue"), * cex=1.5) * ``` ] .pull-right[  ] --- # Interactive plots ```r > library(plotly) > dat = data.frame(x=(0:100)/10,y=x^3-13*x^2+39*x) > plot_ly(data=dat,x=~x,y=~y) ```
--- # Tables If you want to generate a table, make sure it is in .HTML format (instead of Markdown or other formats), e.g., ```r > library(foreign) > titanic3 <- read.dta("Exercises/titanic3.dta") > knitr::kable(head(titanic3[,1:6],n=5), format = "html") ``` <table> <thead> <tr> <th style="text-align:left;"> pclass </th> <th style="text-align:right;"> survived </th> <th style="text-align:left;"> name </th> <th style="text-align:left;"> sex </th> <th style="text-align:right;"> age </th> <th style="text-align:right;"> sibsp </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> 1st </td> <td style="text-align:right;"> 1 </td> <td style="text-align:left;"> Allen, Miss. Elisabeth Walton </td> <td style="text-align:left;"> female </td> <td style="text-align:right;"> 29.0000 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:left;"> 1st </td> <td style="text-align:right;"> 1 </td> <td style="text-align:left;"> Allison, Master. Hudson Trevor </td> <td style="text-align:left;"> male </td> <td style="text-align:right;"> 0.9167 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> 1st </td> <td style="text-align:right;"> 0 </td> <td style="text-align:left;"> Allison, Miss. Helen Loraine </td> <td style="text-align:left;"> female </td> <td style="text-align:right;"> 2.0000 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> 1st </td> <td style="text-align:right;"> 0 </td> <td style="text-align:left;"> Allison, Mr. Hudson Joshua Crei </td> <td style="text-align:left;"> male </td> <td style="text-align:right;"> 30.0000 </td> <td style="text-align:right;"> 1 </td> </tr> <tr> <td style="text-align:left;"> 1st </td> <td style="text-align:right;"> 0 </td> <td style="text-align:left;"> Allison, Mrs. Hudson J C (Bessi </td> <td style="text-align:left;"> female </td> <td style="text-align:right;"> 25.0000 </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- # HTML Widgets ```r > DT::datatable( + head(titanic3[,1:6],n=100), + fillContainer = FALSE, options = list(pageLength = 3) + ) ```
--- class: center, middle # Thanks! Slides created via the R package [**xaringan**](https://github.com/yihui/xaringan).